package com.cheyun.netsalev3.viewmodel.aftersale

import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.aftersale.DriveDetail
import com.cheyun.netsalev3.bean.aftersale.DrivePlanData
import com.cheyun.netsalev3.bean.basedata.Member
import com.cheyun.netsalev3.repository.aftersale.DriveDetailsRepository
import com.cheyun.netsalev3.repository.aftersale.EditDriveRepository
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.ToastUtil
import com.cheyun.netsalev3.view.aftersale.EditDriveActivity
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyDialog
import com.jeremyliao.liveeventbus.LiveEventBus
import java.io.Serializable

/**
 * @author Michael
 * @date  2018/12/19 - 9:41.
 */
class DriveDetailsActivityViewModel(var detailsRepository: DriveDetailsRepository) : BaseViewModel() {

    var detail = ObservableField<DriveDetail>()
    var maintenanceData:DriveDetail? = null
    var phoneMsg = MutableLiveData<String>()
    var editDriveRepository = EditDriveRepository()

    var state = MutableLiveData<Int>()

    var memberlist = ArrayList<Member>()

    init {
        initData()

        MySharedPreferences.getBaseData()?.member?.apply {
            memberlist.addAll(this)
        }
    }

    /**
     * 初始化数据
     */
    fun initData() {
        detailsRepository.getData(this::setData)
    }

    private fun setData(item: DriveDetail) {
        detail.set(item)
        maintenanceData = item
        phoneMsg.postValue(item.phone)
        state.postValue(item.state)
    }

    private var currentView:View? = null
    fun onClickPhone(view: View) {
        currentView = view
        detail.get()?.apply {
            var phone = this.phone
            if(phone.isNullOrEmpty()){
                ToastUtil.toastShortMessage("号码无法拨打")
            }else{
                FunctionUtils.callPhone(currentView?.context!!, detail?.get()?.phone!!)
            }
        }
    }

    fun onClickEdit(view: View){
        var intent = Intent(view.context, EditDriveActivity::class.java)
        maintenanceData.apply {
            intent.putExtra("data", maintenanceData as Serializable)
        }
        view.context.startActivity(intent)
    }

    fun onClickCancelOrder(view: View){
        var dialog = MyContentDialog.newInstance("提示","是否确认取消预约")
        dialog.onClickOk = this@DriveDetailsActivityViewModel::cancelOrder
        dialog.show((view.context as AppCompatActivity).supportFragmentManager, "")
    }
    fun cancelOrder(){
        var moth: () -> Unit = {
            initData()
            LiveEventBus.get().with("aftersale_drive_refresh").postValue("")
        }
        editDriveRepository.cancelOrder(maintenanceData?.id!!,moth)
    }

    fun onClickCancelDrive(view: View){
        var dialog = MyContentDialog.newInstance("提示","是否确认取消代驾订单")
        dialog.onClickOk = this@DriveDetailsActivityViewModel::cancelDrive
        dialog.show((view.context as AppCompatActivity).supportFragmentManager, "")
    }

    fun cancelDrive(){
        var moth: () -> Unit = {
            initData()
            LiveEventBus.get().with("aftersale_drive_refresh").postValue("")
        }
        editDriveRepository.cancelDrive(maintenanceData?.id!!,moth)
    }

    fun onClickShare(view: View){
        var content = view.context
        if (content is AppCompatActivity) {
            var list = ArrayList<DialogParam>()
            for (item in memberlist) {
                if(item.isadmin == 0) {
                    var dialogParam = DialogParam(item.realname, item.uid.toString())
                    list.add(dialogParam)
                }
            }
            var myDialog = MyDialog.newInstance("请选择分配人员", list)
            myDialog.huidia = this::rcvName
            myDialog.show(content.supportFragmentManager, "")
        }
    }

    /**
     * 接待人回调
     */
    fun rcvName(item: DialogParam?) {
        var moth: () -> Unit = {
            MyToast("分配成功")
            initData()
            LiveEventBus.get().with("aftersale_drive_refresh").postValue("")
        }
        var fail: () -> Unit = {

        }
        item?.apply {
            editDriveRepository.allocation(maintenanceData?.id.toString(),item.id,moth,fail)
        }
    }


    var currentPlanData:DrivePlanData? = null
    fun onClickPublishDrive(view: View){
        currentView = view
        editDriveRepository.getPlanPrice(maintenanceData?.id!!,this::getPlanBack)
    }

    fun getPlanBack(planData: DrivePlanData){
        currentPlanData = planData
        var context = currentView?.context
        if (context is AppCompatActivity) {
            var dialog = MyContentDialog.newInstance("提示",
                    "<br>预估费用:<span style='color:red'>"+planData.plantotalfee+"元</span><br>预估里程:<span style='color:red'>"+
                            planData.planmileage + "km</span><br>预估时间:<span style='color:red'>"+planData.plantime+"</span><br>商户剩余金额:<span style='color:red'>"+planData.usableMoney +"元</span><br>",true
            )
            dialog.onClickOk = this@DriveDetailsActivityViewModel::onClickPublish
            dialog.show(context?.supportFragmentManager, "")
        }
    }
    fun onClickPublish(){
        if(currentPlanData!=null) {
            editDriveRepository.drivePublishOrder(currentPlanData?.plantotalfee!!, currentPlanData?.planmileage!!, maintenanceData?.id!!, this::publishBack)
        }
    }

    fun publishBack(){
        initData()
        LiveEventBus.get().with("aftersale_drive_refresh").postValue("")
    }

}